package com.srolorz.fmtp.thread.pool.executor.factory;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;


/**
 * @author eleven
 */
public abstract class AbstractThreadFactory implements ThreadFactory {
    public static final AtomicInteger poolNumber = new AtomicInteger(1);
    public static final Map<String, AtomicInteger> threadGroupMap = new ConcurrentHashMap<>();
    private ThreadGroup threadGroup;
    private String threadNamePrefix;

    public AbstractThreadFactory() {
        SecurityManager s = System.getSecurityManager();
        if(null == this.getThreadGroup()){
            this.setDefaultThreadGroup();
        }
        this.threadNamePrefix = "thread-";
    }

    public ThreadGroup getThreadGroup() {
        return threadGroup;
    }

    public AbstractThreadFactory setThreadGroup(ThreadGroup threadGroup) {
        this.threadGroup = threadGroup;
        return this;
    }

    public AbstractThreadFactory setThreadNamePrefix(String threadNamePrefix) {
        this.threadNamePrefix = threadNamePrefix;
        return this;
    }

    public String getThreadNamePrefix() {
        return threadNamePrefix;
    }

    private void setDefaultThreadGroup(){
        this.threadGroup = new ThreadGroup("fmtp-thread-pool-" + poolNumber.getAndIncrement());
        threadGroupMap.put(this.threadGroup.getName(),new AtomicInteger(1));
    }


}
